package be.artesis.titamo.importer.gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.GroupLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import be.artesis.titamo.exceptions.InvalidEntryException;
import be.artesis.titamo.exceptions.TimeTableParserErrors;

public class ParserErrorDialog extends JDialog
{
	private static final long serialVersionUID = -4882183939853071875L;

	public ParserErrorDialog(JFrame parent, TimeTableParserErrors errors)
	{
		super(parent, "Parser Errors");
		this.setModal(true);
		this.setSize(300,400);
		this.setLocation(parent.getLocation());
		JTextArea errorArea = new JTextArea();
		JScrollPane sp = new JScrollPane(errorArea);
		sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		errorArea.setForeground(Color.RED);
		
		for (InvalidEntryException e : errors.getList())
		{
			errorArea.append("At line " + e.getID() +": " + e.getMessage() + "\n");
		}
		
		JLabel errorLabel = new JLabel("Some parser errors were found...");
		
		errorArea.setEditable(false);
		JPanel labelPanel = new JPanel();
		
		GroupLayout l = new GroupLayout(labelPanel);
		labelPanel.setLayout(l);
		
		l.setHorizontalGroup(
				l.createSequentialGroup()
					.addGap(30)
					.addComponent(errorLabel)
					.addGap(30)
		);
		l.setVerticalGroup(
				l.createSequentialGroup()
					.addGap(30)
					.addComponent(errorLabel)
					.addGap(30)
		);
		
		
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.add(labelPanel, BorderLayout.NORTH);
		mainPanel.add(sp, BorderLayout.CENTER);
	
		this.getContentPane().add(mainPanel);
	}
}
